# from database.lhjy import ali
from utils.pd_show_utils import *
from utils.jsl_data_map import columns_map
from strategy.strategy_filter import filter_base

class StrategyTemplate:
    name = 'DefaultStrategyTemplate'

    def __init__(self, data, top_n=20):
        self.data = data
        self.topN = top_n

    def __init__(self):
        # self.data_tool = ali.rename(columns=columns_map)
        # db = DbHandler()
        # db.create_session()
        # self.db = db
        self.topN = -1
        pass

    def run(self):
        pass

    def api(self,
            db,
            table_name="jsl_bonds_alive",
            filter_base_flag=True,
            fill_na_flag=True):
        self.db = db
        self.data = pd.read_sql_table(table_name, con=self.db.engine)
        #fix sw_cd
        # pd_sw_data = pd.read_sql_table('sw_info', con=db.engine)
        #
        # for item in self.data.iterrows():
        #     sw_cd = int(item[1]['sw_cd'])
        #     fix_data = pd_sw_data.loc[pd_sw_data["行业代码"] == sw_cd]
        #     self.data.loc[item[0], 'sw_nm_r'] = str(fix_data['三级行业名称'].values[0])
        if filter_base_flag:
            self.data = filter_base(self.data)
        if fill_na_flag:
            self.data = self.data.fillna(0)

    def test(self):
        # self.data_tool = ali.rename(columns=columns_map)
        self.run()
        print("策略名称:" + self.strategy_name)
        print("策略描述:" + self.strategy_descrption)
        try:
            df_table(self.data[:self.topN], 'index')
        except KeyError as err:
            print(self.data)
